<?php
$id=!empty($_POST['id']) ? trim($_POST['id']) : null;
$name=!empty($_POST['name']) ? trim($_POST['name']) : null;
$KW=array();
require('inc/MYSQL_Connection.php');
if(mysqli_connect_errno()){
	array_push($KW,array("theme"=>'--数据库连接失败！--',"keyWords"=>''));
}else{
	$db->query("set names utf8");      //解决乱码
	if($id!=null&&$name==null){ 		//通过所属类别id查询
		$query="select ID,theme,keyWords from keywords where category=$id";
		$result=$db->query($query);		
		for($i=0;$i<$result->num_rows;$i++){
			$row=$result->fetch_assoc();			
			array_push($KW,array("ID"=>$row['ID'],"theme"=>$row['theme'],"keyWords"=>$row['keyWords']));
		}
	}
	if($id==null&&$name!=null){ 		//通过所属类别id查询
	    $query="select keywords.ID,name,theme,keyWords from keywords,keywordscategory where keywords.category=keywordscategory.id and theme like '%$name%'";
	    $result=$db->query($query);
	    for($i=0;$i<$result->num_rows;$i++){
	        $row=$result->fetch_assoc();
	        array_push($KW,array("ID"=>$row['ID'],"theme"=>$row['name']."--".$row['theme'],"keyWords"=>$row['keyWords']));
	    }
	}
	$result->free();
	$db->close();
	if(empty($KW))
	    array_push($KW,array("ID"=>0,"theme"=>'--无--',"keyWords"=>''));
}
echo (json_encode($KW));
?>